// index.js
Page({
  data: {
    showPopup: false, //控制支付弹窗是否显示
    selectedItem: {},
    userInfo: {
      avatar: "cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/index/头像.jpg",
      qrUrl: "cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/index/二维码.png",
      stats: [
        {
          id: 1,
          statLabel: "星币",
          statValue: 0
        },
        {
          id: 2,
          statLabel: "星票",
          statValue: 2
        },
        {
          id: 3,
          statLabel: "优惠券",
          statValue: 1
        },
      ]
    },
    products: [
      {
        id: 1,
        image: 'cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/index/拼团500枚.jpg',
        title: '拼团150元得500币',
        timeLeft: '00天15:03:19',
        price: 150,
        sold: 70,
        total: 300
      },
      {
        id: 2,
        image: 'cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/index/拼团1000枚.jpg',
        title: '拼团288元得1000币',
        timeLeft: '00天15:03:20',
        price: 288,
        sold: 75,
        total: 300
      }
    ],
    topUp: [
      {
        id: 1,
        image: "cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/index/1枚星币.jpg",
        title: "1元得1枚星币",
        price: 1,
        oldprice: 1,
      },
      {
        id: 2,
        image: "cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/index/105枚星币.jpg",
        title: "50元得105枚星币",
        price: 50,
        oldprice: 105,
      },
      {
        id: 3,
        image: "cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/index/260枚星币.jpg",
        title: "100元得260枚星币",
        price: 100,
        oldprice: 260,
      },
      {
        id: 4,
        image: "cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/index/1000枚星币.jpg",
        title: "300元得1000枚星币",
        price: 300,
        oldprice: 1000,
      },
      {
        id: 5,
        image: "cloud://cloud1-8g4hfxry284035d7.636c-cloud1-8g4hfxry284035d7-1365437610/images/index/1800枚星币.jpg",
        title: "500元得1800枚星币",
        price: 500,
        oldprice: 1800,
      }
    ]
  },
  openPopup() {
    const comp = this.selectComponent('#popupCard');
    if (comp) {
      comp.openPopup();
    } else {
      console.error('没有找到popupCard组件');
    }
  },
  goToStoredRecord() {
    console.log("fsfds");
    wx.navigateTo({
      url: '/pages/mine/vip/storedRecords/storedRecords',
    })
  },
  onLoad() {

  },
  onShow() {

  },
  goTo(e) {
    const currentUrl = e.currentTarget.dataset.url;
    const isTabBar = e.currentTarget.dataset.isTabBar;
    console.log(isTabBar);
    if (isTabBar) {
      wx.switchTab({
        url: currentUrl,
        fail: (err) => {
          console.error("跳转失败:", err);
          wx.showToast({
            title: '跳转失败',
            icon: 'none'
          });
        }
      });
    }
    else {
      wx.navigateTo({
        url: currentUrl,
        fail: (err) => {
          console.error("跳转失败:", err);
          wx.showToast({
            title: '跳转失败',
            icon: 'none'
          });
        }
      });
    }
  },
  onClick(e) {
    const rawValue = e.currentTarget.dataset.iswhich;
    const tab = Number(rawValue);

    if (isNaN(tab)) {
      console.error("无效的 isWhich 值:", rawValue);
      return;
    }

    console.log(`跳转到 /pages/index/more/more?currentTab=${tab}`);

    wx.navigateTo({
      url: `/pages/index/more/more?currentTab=${tab}`,
      fail: (err) => {
        console.error("跳转失败:", err);
        wx.showToast({
          title: '跳转失败',
          icon: 'none'
        });
      }
    });
  },
  // 用户点击商品，弹出弹窗
  onSelectGoods(e) {
    const item = e.currentTarget.dataset.item;
    console.log(item);
    const rawValue = e.currentTarget.dataset.iswhich;
    const tab = Number(rawValue);
    if (isNaN(tab)) {
      console.error("无效的 isWhich 值:", rawValue);
      return;
    }
    if (tab === 0) {
      const isTimeLimit = true;
      const encodedGoods = encodeURIComponent(JSON.stringify(item));
      wx.navigateTo({
        url: `/pages/common/goods-details/goods-details?goods=${encodedGoods}&isTimeLimit=${isTimeLimit}`,
        fail: (err) => {
          console.error("跳转失败:", err);
          wx.showToast({
            title: '跳转失败',
            icon: 'none'
          });
        }
      });
    }
    if (tab === 1) {
      this.setData({
        selectedItem: { ...item },  // 拷贝数据避免影响原列表
        showPopup: true
      });
    }
  },

  // 关闭弹窗
  onClosePopup() {
    this.setData({ showPopup: false });
  },

  // 用户点击“立即支付”
  onPay(e) {
    const { item, total } = e.detail;
    console.log('发起支付：', item, '总价：', total);
    wx.showToast({ title: '已模拟下单', icon: 'success' });
    this.setData({ showPopup: false });
  }
})
